package ssen.mvc.ondisplay {
import avmplus.getQualifiedClassName;

import flash.display.DisplayObject;
import flash.utils.Dictionary;

import ssen.mvc.core.IInjector;
import ssen.mvc.core.IViewInjector;

public class DisplayViewInjector implements IViewInjector {
	private var mediatorMap:Dictionary;
	private var injector:IInjector;

	public function DisplayViewInjector(injector:IInjector) {
		this.injector=injector;
		mediatorMap=new Dictionary;
	}

	public function dispose():void {
		mediatorMap=null;
		injector=null;
	}

	public function hasMapping(view:*):Boolean {
		if (view is Class) {
			return mediatorMap[view] !== undefined;
		}

		return mediatorMap[view["constructor"]] !== undefined;
	}

	public function injectInto(view:Object):void {
		injector.injectInto(view);

		if (mediatorMap[view["constructor"]] is Class) {
			new DisplayMediatorController(injector, view as DisplayObject, mediatorMap[view["constructor"]]);
		}
	}

	public function mapView(viewClass:Class, mediatorClass:Class=null):void {
		if (mediatorMap[viewClass] !== undefined) {
			throw new Error(getQualifiedClassName(viewClass) + " is mapped!!!");
		}
		mediatorMap[viewClass]=mediatorClass;
	}

}
}
